// components/customTabBar.js
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    tab_bar_data:{},
    currentIndex:0
  },
  attached(){
      this.resetTabBarData();
  },
  /**
   * 组件的方法列表
   */
  methods: {
    resetTabBarData: function () {
      var _curPageArr = getCurrentPages();
      console.log('getCurrentPages',getCurrentPages());
      var _curPage = _curPageArr[_curPageArr.length - 1];
      var _pagePath = _curPage.__route__;
      if (_pagePath.indexOf('/') != 0) {
        _pagePath = '/' + _pagePath;
      }
      var tabBar = app.globalData.tabBar;
      for (var i = 0; i < tabBar.list.length; i++) {
        tabBar.list[i].selected = false;
        if (tabBar.list[i].pagePath == _pagePath) {
          tabBar.list[i].selected = true;//根据页面地址设置当前页面状态
          this.setData({
            currentIndex:i
          })
        }
      }
      this.setData({
        tab_bar_data: tabBar
      });
    },
    selectTab:function (e) {
      console.log('1111')
      var curritem = e.currentTarget.dataset.curritem;
      console.log('this.data.currentIndex',this.data.currentIndex,curritem.currIndex);
      wx.redirectTo({//参考很多小程序tabBar,用redirectTo是最合适的，并且没有bug，不会增加getCurrentPages的栈溢出
            url: curritem.pagePath
      })
    },
  }
})
